//
//  FCRelationCompanyDetailViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/22.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCRelationCompanyDetailViewController.h"
#import "CustomerRelationHeader.h"
#import "UIImage+imageExtension.h"
#import "FCBtnTypeRuleView.h"
#import "FCRelationBindCompanyInfoTableViewCell.h"
#import "FCRelationDetailEmployeeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FCCustomerRelationRequest.h"
#import "FCRelationTyleSheetViewController.h"
#import "FCCompanyCardTool.h"
#import "UIViewController+runtimeCheck.h"
#import "MJExtension.h"
#import "FCRelationBindRequestViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "FCBaseTool.h"
#import "FCGzipAndBase64Encoded.h"
#import "FCModel2Json.h"
#import "FCAlertViewController.h"
#import "FCCrmSupplyRelationOnlyTypeManagerModel.h"

@interface FCRelationCompanyDetailViewController () <btnTypeRuleDelegate, UITableViewDelegate, UITableViewDataSource>
//@property (nonatomic, strong) FCCompanyCardToolBar *toolBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *relationType;
@property (nonatomic, strong) UIView *bgview;
@property (nonatomic, strong) FCBtnTypeRuleView *bottomReviewView;

@property (nonatomic, strong) NSMutableArray *selectedRelationTypeArr;
@property (nonatomic, strong) NSMutableArray *relationTypes;
@property (nonatomic, strong) NSMutableArray *manageRelationTypes;
@property (nonatomic, assign) BOOL isLoadData;

@end

@implementation FCRelationCompanyDetailViewController
static UIImageView *telImageView;

- (NSMutableArray *)selectedRelationTypeArr{
    if (!_selectedRelationTypeArr) {
        _selectedRelationTypeArr = [NSMutableArray array];
    }
    return _selectedRelationTypeArr;
}
- (NSMutableArray *)relationTypes{
    if (!_relationTypes) {
        _relationTypes = [NSMutableArray array];
    }
    return _relationTypes;
}
- (NSMutableArray *)manageRelationTypes{
    if (!_manageRelationTypes) {
        _manageRelationTypes = [NSMutableArray array];
    }
    return _manageRelationTypes;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setTabbarColor];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

#pragma mark - 设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color
{
    UIView *statusBar;
    if (@available(iOS 13.0, *)) {
        UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
        if ([statusBarManager respondsToSelector:@selector(createLocalStatusBar)]) {
            UIView *_localStatusBar = [statusBarManager performSelector:@selector(createLocalStatusBar)];
            if ([_localStatusBar respondsToSelector:@selector(statusBar)]) {
                statusBar = [_localStatusBar performSelector:@selector(statusBar)];
            }
        }
    } else {
        // Fallback on earlier versions
        statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    }
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    UIColor *alphaColor = [UIColor whiteColor];
    UIImage *alphaImage = [UIImage imageWithColor:alphaColor];
    [self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:FCTxtColor}];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)setTabbarColor{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, WJCScreenW, 64);
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:81/255.0 green:167/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:35/255.0 green:142/255.0 blue:250/255.0 alpha:1.0].CGColor];
    gradientLayer.locations = @[@(0), @(1.0f)];
    UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.opaque, 0);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *alphaImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTabbarColor];
    if (!self.relation) {
        [self getRelationData];
    }
    [self setupNavi];
    [self setupUI];
}

- (void)setupNavi{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithiImage:customerRelationImageName(@"nav_ic_arrow_w") target:self action:@selector(backItemClick)];
    self.bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WJCScreenW, WJCScreenH - FCTabbarHeight - FCNavBarHeight)];
    _bgview.backgroundColor = FCBackColor;
    [self.view addSubview:_bgview];
    if ([self.relation.status isEqualToString:@"Agree"]) {
        _bgview.height = WJCScreenH;
    } else {
        _bgview.height = WJCScreenH - FCTabbarHeight - FCNavBarHeight;
    }
    
    UIView *backView = [[UIView alloc]init];
    backView.frame = CGRectMake(0, 0, WJCScreenW, 104);
    backView.backgroundColor = FCBlueBackColor;
    [self.view addSubview:backView];
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = backView.frame;
    [backView.layer addSublayer:gl];
    gl.startPoint = CGPointMake(0, 1);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:81/255.0 green:167/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:35/255.0 green:142/255.0 blue:250/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
}
- (void)backItemClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI{
    if (self.isMe) {
        self.title = self.relation.me.Name ?: self.relation.relCompanyName;
    } else{
        self.title = self.relation.rel.Name ?: self.relation.relCompanyName;
    }
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    self.bottomReviewView = [[FCBtnTypeRuleView alloc] initWithFrame:CGRectMake(0, 0, WJCScreenW, 0)];
    [self.view addSubview:_bottomReviewView];
    if ([self.relation.status isEqualToString:@"Agree"]) {
        _bottomReviewView.height = 0.01;
        _bottomReviewView.y = WJCScreenH - FCTabbarSafeBottomMargin - 0.01;
    } else {
        _bottomReviewView.height = 80;
        _bottomReviewView.y = WJCScreenH - 80 - 64 - FCTabbarSafeBottomMargin;
    }
    _bottomReviewView.backgroundColor = [UIColor whiteColor];
    _bottomReviewView.ruleType = ruleAverageRadius;
    _bottomReviewView.btnArr = [NSMutableArray arrayWithObjects:[btnTypeModel itemWithTitle:customerRelationLocaChanged(@"common_emun_delete_enterprise", @"删除企业") btnImage:nil detail:nil btnType:btnNomal itemType:0], [btnTypeModel itemWithTitle:customerRelationLocaChanged(@"重新发送绑定请求", @"重新发送绑定请求") btnImage:nil detail:nil btnType:btnNomal itemType:1], nil];
    _bottomReviewView.delegate = self;
//    BA_ViewBorderRadius(bottomReviewView, 0, 0.5, FCLineColor);
    for (UIButton *subBtn in _bottomReviewView.subviews) {
        subBtn.y = 12;
        subBtn.height = 42;
        if ([subBtn.titleLabel.text isEqualToString:customerRelationLocaChanged(@"common_emun_delete_enterprise", @"删除企业")]) {
            BA_ViewBorderRadius(subBtn, 2, 1, FCLineColor);
            [subBtn setTitleColor:FCRedBackColor forState:UIControlStateNormal];
        } else{
            subBtn.backgroundColor = FCBlueBackColor;
            BA_ViewBorderRadius(subBtn, 2, 0, FCLineColor);
            [subBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _bottomReviewView.width, 0.5)];
    lineView.backgroundColor = FCLineColor;
    [_bottomReviewView addSubview:lineView];
    
    WJCWeakSelf
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        if ([weakSelf.relation.status isEqualToString:@"Agree"]) {
            make.bottom.equalTo(weakSelf.bottomReviewView.mas_top).mas_equalTo(-64);
        } else {
            make.bottom.equalTo(weakSelf.bottomReviewView.mas_top).mas_equalTo(0);
        }
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    [self setupTableView];
}

-(void)btnTypeRuleClickWithBtn:(UIButton*)sender andView:(FCBtnTypeRuleView*)view{
    if ([sender.titleLabel.text isEqualToString:customerRelationLocaChanged(@"common_emun_delete_enterprise", @"删除企业")]) {
        [self deleteRelationCompany];
    }
    if ([sender.titleLabel.text isEqualToString:customerRelationLocaChanged(@"重新发送绑定请求", @"重新发送绑定请求")]) {
        [self resetRelationRequest];
    }
}

#pragma mark - tableView
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.sectionHeaderHeight = 12;
    self.tableView.sectionFooterHeight = 0;
    [self setExtraCellLineHidden:self.tableView];
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(!self.relation){
        return 3;
    }
    if (self.isMe) {
        if (self.relation.me && self.relation) {
            return 3;
        } else if(self.relation.me ){
            return 2;
        } else{
            return 1;
        }
    } else{
        if (self.relation.rel && self.relation.relRelation) {
            return 3;
        } else if(self.relation.rel ){
            return 2;
        } else{
            return 1;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = UIColor.clearColor;
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.relation) {
        if (indexPath.section == 0) {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyInfo"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
            UIImageView *avatarImageView = [UIImageView new];
            [cell.contentView addSubview:avatarImageView];
            [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.and.top.mas_equalTo(16);
                make.height.mas_equalTo(48);
                make.width.mas_equalTo(48);
            }];
            
            if (self.relation.me.LogoUrl.length) {
                [avatarImageView sd_setImageWithURL:[NSURL URLWithString:self.relation.me.LogoUrl] placeholderImage:LogoPlaceImage];
            } else{
                UILabel *logoImageLabel = [UILabel new];
                [avatarImageView addSubview:logoImageLabel];
                [logoImageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.and.top.and.right.and.bottom.mas_equalTo(0);
                }];
                FCSETLRViewBorderRadius(logoImageLabel, 24);
                logoImageLabel.font = font12;
                logoImageLabel.textColor = FCWhiteColor;
                logoImageLabel.textAlignment = NSTextAlignmentCenter;
                
                FCRelationTypeModel *relationType = self.relation.types.firstObject;
                if ([relationType.code isEqualToString:@"processor"]) {
                    // 加工商
                    logoImageLabel.backgroundColor = FCRedBackColor;
                    logoImageLabel.alpha = 0.6;
                } else if([relationType.code isEqualToString:@"supply"]){
                    // 供应商
                    logoImageLabel.backgroundColor = FCOrangeColor;
                    logoImageLabel.alpha = 0.6;
                } else if([relationType.code isEqualToString:@"purchase"]){
                    // 供应商
                    logoImageLabel.backgroundColor = FCBlueTxtColor;
                    logoImageLabel.alpha = 0.6;
                } else if([relationType.code isEqualToString:@"server"]){
                    // 供应商
                    logoImageLabel.backgroundColor = HEXCOLOR(0x1FA679);
                    logoImageLabel.alpha = 0.6;
                } else{
                    // 其它
                    logoImageLabel.backgroundColor = HEXCOLOR(0xB87333);
                    logoImageLabel.alpha = 0.4;
                }
                if (self.relation.me.BrandName.length > 2) {
                    NSString *logoStr = [self.relation.me.BrandName substringToIndex:2];
                    logoImageLabel.text = logoStr;
                } else{
                    logoImageLabel.text = self.relation.me.BrandName;
                }
            }
            
            UILabel *statusLabel = [UILabel new];
            [cell.contentView addSubview:statusLabel];
            [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-16);
                make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                make.height.mas_equalTo(22);
            }];
            statusLabel.font = font14;
            if ([self.relation.status isEqualToString:@"Request"]) {
                statusLabel.textColor = FCBlueTxtColor;
                statusLabel.text = customerRelationLocaChanged(@"已发送绑定请求", @"已发送绑定请求");
            }
            if ([self.relation.status isEqualToString:@"Reject"]) {
                statusLabel.textColor = FCRedTxtColor;
                statusLabel.text = customerRelationLocaChanged(@"请求被驳回", @"请求被驳回");
            }
            
            UILabel *companyNameLabel = [UILabel new];
            [cell.contentView addSubview:companyNameLabel];
            [companyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(statusLabel.mas_left).mas_equalTo(-9);
                make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                make.left.equalTo(avatarImageView.mas_right).mas_equalTo(9);
                make.height.mas_equalTo(22);
            }];
            companyNameLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
            companyNameLabel.textColor = FCTxtColor;
            companyNameLabel.text = self.relation.me.BrandName ?: @"";
            companyNameLabel.numberOfLines = 1;
            
            UILabel *mainBusinessLabel = [UILabel new];
            [cell.contentView addSubview:mainBusinessLabel];
            [mainBusinessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(companyNameLabel.mas_bottom).mas_equalTo(2);
                make.left.equalTo(companyNameLabel.mas_left).mas_equalTo(0);
                make.right.mas_equalTo(-16);
            }];
            mainBusinessLabel.font = font14;
            mainBusinessLabel.textColor = FCGrayTxtDetailColor;
            mainBusinessLabel.numberOfLines = 0;
            if (self.relation.me.CustomMainBusiness.length > 0) {
                mainBusinessLabel.text = [NSString stringWithFormat:@"%@%@", customerRelationLocaChanged(@"主营：", @"主营："), self.relation.me.CustomMainBusiness];
            } else{
                mainBusinessLabel.text = customerRelationLocaChanged(@"主营：老板生意很好，还没添加主营信息", @"主营：老板生意很好，还没添加主营信息");
            }
            
            UIView *lineView1 = [UIView new];
            [cell.contentView addSubview:lineView1];
            [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(avatarImageView.mas_bottom).mas_equalTo(16);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(1);
            }];
            lineView1.backgroundColor = FCBackColor;
            
            UIView *editRelationTypeView = [UIView new];
            [cell.contentView addSubview:editRelationTypeView];
            [editRelationTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lineView1.mas_bottom).mas_equalTo(0);
                make.left.and.right.mas_equalTo(0);
                if (IphonePlus) {
                    make.height.mas_offset(54);
                } else{
                    make.height.mas_equalTo(48);
                }
            }];
            UITapGestureRecognizer *editRelationTypeTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editRelationTypeAction:)];
            [editRelationTypeView addGestureRecognizer:editRelationTypeTapGesture];
            
            UIButton *relationTypeEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [editRelationTypeView addSubview:relationTypeEditButton];
            [relationTypeEditButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-16);
                make.width.mas_equalTo(46);
                make.height.mas_equalTo(16);
                make.centerY.mas_offset(0);
            }];
            relationTypeEditButton.titleLabel.font = font10;
            [relationTypeEditButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            [relationTypeEditButton setTitle:customerRelationLocaChanged(@"employee_department_edti", @"编辑") forState:UIControlStateNormal];
            [relationTypeEditButton setImage:customerRelationImageName(@"bt_edit") forState:UIControlStateNormal];
            relationTypeEditButton.backgroundColor = FCBlueBackColor;
            relationTypeEditButton.layer.cornerRadius = 2;
            [relationTypeEditButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - relationTypeEditButton.imageView.frame.size.width, 0, relationTypeEditButton.imageView.frame.size.width)];
            [relationTypeEditButton setImageEdgeInsets:UIEdgeInsetsMake(0, relationTypeEditButton.titleLabel.bounds.size.width + 3, 0, - relationTypeEditButton.titleLabel.bounds.size.width)];
            [relationTypeEditButton addTarget:self action:@selector(editRelationTypeAction:) forControlEvents:UIControlEventTouchUpInside];
            
            _relationType = [UILabel new];
            [editRelationTypeView addSubview:_relationType];
            [_relationType mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(relationTypeEditButton).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.equalTo(relationTypeEditButton.mas_left).mas_equalTo(-10);
            }];
            _relationType.font = font14;
            _relationType.textColor = FCTxtColor;
            _relationType.numberOfLines = 1;
            NSString *typeStr = @"";
            for (FCRelationTypeModel *typeModel in self.relation.types) {
                
                typeStr = [NSString stringWithFormat:@"%@|%@",typeStr, typeModel.name];
            }
            if (typeStr.length > 1) {
                typeStr = [typeStr substringFromIndex:1];
            }
            _relationType.text = typeStr;
            
            UIView *lineView2 = [UIView new];
            [cell.contentView addSubview:lineView2];
            [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(editRelationTypeView.mas_bottom).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(1);
            }];
            lineView2.backgroundColor = FCBackColor;
            
            UIView *addressView = [UIView new];
            [cell.contentView addSubview:addressView];
            [addressView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lineView2.mas_bottom).mas_equalTo(0);
                make.left.and.right.mas_equalTo(0);
                if (IphonePlus) {
                    make.height.mas_offset(54);
                } else{
                    make.height.mas_equalTo(48);
                }
            }];
            UITapGestureRecognizer *addressTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushNav)];
            [addressView addGestureRecognizer:addressTapGesture];
            
            UIButton *addressButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [addressView addSubview:addressButton];
            [addressButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.right.mas_equalTo(-16);
                make.width.mas_equalTo(46);
                make.height.mas_equalTo(16);
            }];
            addressButton.titleLabel.font = font10;
            [addressButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            [addressButton setTitle:customerRelationLocaChanged(@"global_location_location_navigate", @"导航") forState:UIControlStateNormal];
            [addressButton setImage:customerRelationImageName(@"bt_nav") forState:UIControlStateNormal];
            addressButton.backgroundColor = FCBlueBackColor;
            addressButton.layer.cornerRadius = 2;
            [addressButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - addressButton.imageView.frame.size.width, 0, addressButton.imageView.frame.size.width)];
            [addressButton setImageEdgeInsets:UIEdgeInsetsMake(0, addressButton.titleLabel.bounds.size.width + 3, 0, - addressButton.titleLabel.bounds.size.width)];
            [addressButton addTarget:self action:@selector(pushNav) forControlEvents:UIControlEventTouchUpInside];
            
            UILabel *address = [UILabel new];
            [addressView addSubview:address];
            [address mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(addressButton).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.equalTo(addressButton.mas_left).mas_equalTo(-10);
            }];
            address.font = font14;
            address.textColor = FCTxtColor;
            address.numberOfLines = 1;
            address.text = [NSString stringWithFormat:@"%@%@%@%@%@",self.relation.me.ResidentCountry ?: @"",self.relation.me.ResidentProvince ?: @"",self.relation.me.ResidentCity ?: @"",self.relation.me.ResidentArea ?: @"",self.relation.me.Address ?: @""];
            
            UIView *lineView3 = [UIView new];
            [cell.contentView addSubview:lineView3];
            [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(addressView.mas_bottom).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(1);
            }];
            lineView3.backgroundColor = FCBackColor;
            
            UIView *phoneView = [UIView new];
            [cell.contentView addSubview:phoneView];
            [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lineView3.mas_bottom).mas_equalTo(0);
                make.left.and.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                if (IphonePlus) {
                    make.height.mas_offset(54);
                } else{
                    make.height.mas_equalTo(48);
                }
            }];
            UITapGestureRecognizer *phoneTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushTelPhone)];
            [phoneView addGestureRecognizer:phoneTapGesture];
            
            UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [phoneView addSubview:phoneButton];
            [phoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.right.mas_equalTo(-16);
                make.width.mas_equalTo(60);
                make.height.mas_equalTo(16);
            }];
            phoneButton.titleLabel.font = font10;
            [phoneButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            [phoneButton setTitle:customerRelationLocaChanged(@"company_card_takephone", @"打电话") forState:UIControlStateNormal];
            [phoneButton setImage:customerRelationImageName(@"bt_call") forState:UIControlStateNormal];
            phoneButton.backgroundColor = FCBlueBackColor;
            phoneButton.layer.cornerRadius = 2;
            [phoneButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - phoneButton.imageView.frame.size.width, 0, phoneButton.imageView.frame.size.width)];
            [phoneButton setImageEdgeInsets:UIEdgeInsetsMake(0, phoneButton.titleLabel.bounds.size.width + 3, 0, - phoneButton.titleLabel.bounds.size.width)];
            [phoneButton addTarget:self action:@selector(pushTelPhone) forControlEvents:UIControlEventTouchUpInside];
            
            UIView *telView = [FCCompanyCardTool telPhoneWithCompanyModelOwnerIdentityModel:self.relation.me];
            [phoneView addSubview:telView];
            [telView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(phoneButton).mas_equalTo(0);
                make.left.mas_equalTo(16);
                make.right.equalTo(phoneButton.mas_left).mas_equalTo(-10);
                make.height.mas_equalTo(20);
            }];
            
            return cell;
        }
        if (indexPath.section == 1) {
            UITableViewCell *cell = [[UITableViewCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
            cell.backgroundColor = FCWhiteColor;
            
            UILabel *titleLabel = [UILabel new];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.left.mas_equalTo(16);
            }];
            titleLabel.font = Bfont17;
            titleLabel.textColor = FCTxtColor;
            titleLabel.text = @"企业联系人";
            
            UIView *view = [UIView new];
            [cell.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleLabel.mas_bottom).mas_equalTo(10);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.bottom.mas_equalTo(-16);
                make.height.mas_equalTo(100);
            }];
            
            return cell;
        }
        if (indexPath.section == 2) {
            UITableViewCell *cell = [[UITableViewCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
            cell.backgroundColor = FCWhiteColor;
            
            UILabel *titleLabel = [UILabel new];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.left.mas_equalTo(16);
            }];
            titleLabel.font = Bfont17;
            titleLabel.textColor = FCTxtColor;
            titleLabel.text = @"我司指派联系人";
            
            UIView *view = [UIView new];
            [cell.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleLabel.mas_bottom).mas_equalTo(10);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.bottom.mas_equalTo(-16);
                make.height.mas_equalTo(100);
            }];
            
            UIButton *addRelationEmployeeButton = [UIButton new];
            [view addSubview:addRelationEmployeeButton];
            [addRelationEmployeeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(36);
                make.bottom.mas_equalTo(0);
            }];
            addRelationEmployeeButton.backgroundColor = FCBackColor;
            [addRelationEmployeeButton setTitle:customerRelationLocaChanged(@"+ 指派新的联系人", @"+ 指派新的联系人") forState:UIControlStateNormal];
            [addRelationEmployeeButton setTitleColor:FCBlueTxtColor forState:UIControlStateNormal];
            addRelationEmployeeButton.titleLabel.font = font14;
            addRelationEmployeeButton.layer.cornerRadius = 4;
            
            return cell;
        }
    }
    
    if (indexPath.section == 0) {
        if (self.isMe) {
            if (self.relation.me) {
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyInfo"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
                UIImageView *avatarImageView = [UIImageView new];
                [cell.contentView addSubview:avatarImageView];
                [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.and.top.mas_equalTo(16);
                    make.height.mas_equalTo(48);
                    make.width.mas_equalTo(48);
                }];
                
                if (self.relation.me.LogoUrl.length) {
                    [avatarImageView sd_setImageWithURL:[NSURL URLWithString:self.relation.me.LogoUrl] placeholderImage:LogoPlaceImage];
                } else{
                    UILabel *logoImageLabel = [UILabel new];
                    [avatarImageView addSubview:logoImageLabel];
                    [logoImageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.and.top.and.right.and.bottom.mas_equalTo(0);
                    }];
                    FCSETLRViewBorderRadius(logoImageLabel, 24);
                    logoImageLabel.font = font12;
                    logoImageLabel.textColor = FCWhiteColor;
                    logoImageLabel.textAlignment = NSTextAlignmentCenter;
                    
                    FCRelationTypeModel *relationType = self.relation.types.firstObject;
                    if ([relationType.code isEqualToString:@"processor"]) {
                        // 加工商
                        logoImageLabel.backgroundColor = FCRedBackColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"supply"]){
                        // 供应商
                        logoImageLabel.backgroundColor = FCOrangeColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"purchase"]){
                        // 供应商
                        logoImageLabel.backgroundColor = FCBlueTxtColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"server"]){
                        // 供应商
                        logoImageLabel.backgroundColor = HEXCOLOR(0x1FA679);
                        logoImageLabel.alpha = 0.6;
                    } else{
                        // 其它
                        logoImageLabel.backgroundColor = HEXCOLOR(0xB87333);
                        logoImageLabel.alpha = 0.4;
                    }
                    if (self.relation.me.BrandName.length > 2) {
                        NSString *logoStr = [self.relation.me.BrandName substringToIndex:2];
                        logoImageLabel.text = logoStr;
                    } else{
                        logoImageLabel.text = self.relation.me.BrandName;
                    }
                }
                
                UILabel *statusLabel = [UILabel new];
                [cell.contentView addSubview:statusLabel];
                [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-16);
                    make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                    make.height.mas_equalTo(22);
                }];
                statusLabel.font = font14;
                if ([self.relation.status isEqualToString:@"Request"]) {
                    statusLabel.textColor = FCBlueTxtColor;
                    statusLabel.text = customerRelationLocaChanged(@"已发送绑定请求", @"已发送绑定请求");
                }
                if ([self.relation.status isEqualToString:@"Reject"]) {
                    statusLabel.textColor = FCRedTxtColor;
                    statusLabel.text = customerRelationLocaChanged(@"请求被驳回", @"请求被驳回");
                }
                
                UILabel *companyNameLabel = [UILabel new];
                [cell.contentView addSubview:companyNameLabel];
                [companyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(statusLabel.mas_left).mas_equalTo(-9);
                    make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                    make.left.equalTo(avatarImageView.mas_right).mas_equalTo(9);
                    make.height.mas_equalTo(22);
                }];
                companyNameLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
                companyNameLabel.textColor = FCTxtColor;
                companyNameLabel.text = self.relation.me.BrandName ?: @"";
                companyNameLabel.numberOfLines = 1;
                
                UILabel *mainBusinessLabel = [UILabel new];
                [cell.contentView addSubview:mainBusinessLabel];
                [mainBusinessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(companyNameLabel.mas_bottom).mas_equalTo(2);
                    make.left.equalTo(companyNameLabel.mas_left).mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                }];
                mainBusinessLabel.font = font14;
                mainBusinessLabel.textColor = FCGrayTxtDetailColor;
                mainBusinessLabel.numberOfLines = 0;
                if (self.relation.me.CustomMainBusiness.length > 0) {
                    mainBusinessLabel.text = [NSString stringWithFormat:@"%@%@", customerRelationLocaChanged(@"主营：", @"主营："), self.relation.me.CustomMainBusiness];
                } else{
                    mainBusinessLabel.text = customerRelationLocaChanged(@"主营：老板生意很好，还没添加主营信息", @"主营：老板生意很好，还没添加主营信息");
                }
                
                UIView *lineView1 = [UIView new];
                [cell.contentView addSubview:lineView1];
                [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(avatarImageView.mas_bottom).mas_equalTo(16);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView1.backgroundColor = FCBackColor;
                
                UIView *editRelationTypeView = [UIView new];
                [cell.contentView addSubview:editRelationTypeView];
                [editRelationTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView1.mas_bottom).mas_equalTo(0);
                    make.left.and.right.mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                }];
                UITapGestureRecognizer *editRelationTypeTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editRelationTypeAction:)];
                [editRelationTypeView addGestureRecognizer:editRelationTypeTapGesture];
                
                UIButton *relationTypeEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [editRelationTypeView addSubview:relationTypeEditButton];
                [relationTypeEditButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(46);
                    make.height.mas_equalTo(16);
                    make.centerY.mas_offset(0);
                }];
                relationTypeEditButton.titleLabel.font = font10;
                [relationTypeEditButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [relationTypeEditButton setTitle:customerRelationLocaChanged(@"employee_department_edti", @"编辑") forState:UIControlStateNormal];
                [relationTypeEditButton setImage:customerRelationImageName(@"bt_edit") forState:UIControlStateNormal];
                relationTypeEditButton.backgroundColor = FCBlueBackColor;
                relationTypeEditButton.layer.cornerRadius = 2;
                [relationTypeEditButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - relationTypeEditButton.imageView.frame.size.width, 0, relationTypeEditButton.imageView.frame.size.width)];
                [relationTypeEditButton setImageEdgeInsets:UIEdgeInsetsMake(0, relationTypeEditButton.titleLabel.bounds.size.width + 3, 0, - relationTypeEditButton.titleLabel.bounds.size.width)];
                [relationTypeEditButton addTarget:self action:@selector(editRelationTypeAction:) forControlEvents:UIControlEventTouchUpInside];
                
                _relationType = [UILabel new];
                [editRelationTypeView addSubview:_relationType];
                [_relationType mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(relationTypeEditButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(relationTypeEditButton.mas_left).mas_equalTo(-10);
                }];
                _relationType.font = font14;
                _relationType.textColor = FCTxtColor;
                _relationType.numberOfLines = 1;
                NSString *typeStr = @"";
                for (FCRelationTypeModel *typeModel in self.relation.types) {
                    
                    typeStr = [NSString stringWithFormat:@"%@|%@",typeStr, typeModel.name];
                }
                if (typeStr.length > 1) {
                    typeStr = [typeStr substringFromIndex:1];
                }
                _relationType.text = typeStr;
                
                UIView *lineView2 = [UIView new];
                [cell.contentView addSubview:lineView2];
                [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(editRelationTypeView.mas_bottom).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView2.backgroundColor = FCBackColor;
                
                UIView *addressView = [UIView new];
                [cell.contentView addSubview:addressView];
                [addressView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView2.mas_bottom).mas_equalTo(0);
                    make.left.and.right.mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                }];
                UITapGestureRecognizer *addressTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushNav)];
                [addressView addGestureRecognizer:addressTapGesture];
                
                UIButton *addressButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [addressView addSubview:addressButton];
                [addressButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(46);
                    make.height.mas_equalTo(16);
                }];
                addressButton.titleLabel.font = font10;
                [addressButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [addressButton setTitle:customerRelationLocaChanged(@"global_location_location_navigate", @"导航") forState:UIControlStateNormal];
                [addressButton setImage:customerRelationImageName(@"bt_nav") forState:UIControlStateNormal];
                addressButton.backgroundColor = FCBlueBackColor;
                addressButton.layer.cornerRadius = 2;
                [addressButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - addressButton.imageView.frame.size.width, 0, addressButton.imageView.frame.size.width)];
                [addressButton setImageEdgeInsets:UIEdgeInsetsMake(0, addressButton.titleLabel.bounds.size.width + 3, 0, - addressButton.titleLabel.bounds.size.width)];
                [addressButton addTarget:self action:@selector(pushNav) forControlEvents:UIControlEventTouchUpInside];
                
                UILabel *address = [UILabel new];
                [addressView addSubview:address];
                [address mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(addressButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(addressButton.mas_left).mas_equalTo(-10);
                }];
                address.font = font14;
                address.textColor = FCTxtColor;
                address.numberOfLines = 1;
                address.text = [NSString stringWithFormat:@"%@%@%@%@%@",self.relation.me.ResidentCountry ?: @"",self.relation.me.ResidentProvince ?: @"",self.relation.me.ResidentCity ?: @"",self.relation.me.ResidentArea ?: @"",self.relation.me.Address ?: @""];
                
                UIView *lineView3 = [UIView new];
                [cell.contentView addSubview:lineView3];
                [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(addressView.mas_bottom).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView3.backgroundColor = FCBackColor;
                
                UIView *phoneView = [UIView new];
                [cell.contentView addSubview:phoneView];
                [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView3.mas_bottom).mas_equalTo(0);
                    make.left.and.right.mas_equalTo(0);
                    make.bottom.mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                }];
                UITapGestureRecognizer *phoneTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushTelPhone)];
                [phoneView addGestureRecognizer:phoneTapGesture];
                
                UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [phoneView addSubview:phoneButton];
                [phoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(16);
                }];
                phoneButton.titleLabel.font = font10;
                [phoneButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [phoneButton setTitle:customerRelationLocaChanged(@"company_card_takephone", @"打电话") forState:UIControlStateNormal];
                [phoneButton setImage:customerRelationImageName(@"bt_call") forState:UIControlStateNormal];
                phoneButton.backgroundColor = FCBlueBackColor;
                phoneButton.layer.cornerRadius = 2;
                [phoneButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - phoneButton.imageView.frame.size.width, 0, phoneButton.imageView.frame.size.width)];
                [phoneButton setImageEdgeInsets:UIEdgeInsetsMake(0, phoneButton.titleLabel.bounds.size.width + 3, 0, - phoneButton.titleLabel.bounds.size.width)];
                [phoneButton addTarget:self action:@selector(pushTelPhone) forControlEvents:UIControlEventTouchUpInside];
                
                UIView *telView = [FCCompanyCardTool telPhoneWithCompanyModelOwnerIdentityModel:self.relation.me];
                [phoneView addSubview:telView];
                [telView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(phoneButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(phoneButton.mas_left).mas_equalTo(-10);
                    make.height.mas_equalTo(20);
                }];
                
                return cell;
            } else {
                return [self meRelationCellWithIndexPath:indexPath.row];
            }
        } else {
            if (self.relation.rel) {
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyInfo"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
                UIImageView *avatarImageView = [UIImageView new];
                [cell.contentView addSubview:avatarImageView];
                [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.and.top.mas_equalTo(16);
                    make.height.mas_equalTo(48);
                    make.width.mas_equalTo(48);
                }];
                
                if (self.relation.rel.LogoUrl.length) {
                    [avatarImageView sd_setImageWithURL:[NSURL URLWithString:self.relation.rel.LogoUrl] placeholderImage:LogoPlaceImage];
                } else{
                    UILabel *logoImageLabel = [UILabel new];
                    [avatarImageView addSubview:logoImageLabel];
                    [logoImageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.and.top.and.right.and.bottom.mas_equalTo(0);
                    }];
                    FCSETLRViewBorderRadius(logoImageLabel, 24);
                    logoImageLabel.font = font12;
                    logoImageLabel.textColor = FCWhiteColor;
                    logoImageLabel.textAlignment = NSTextAlignmentCenter;
                    
                    FCRelationTypeModel *relationType = self.relation.types.firstObject;
                    if ([relationType.code isEqualToString:@"processor"]) {
                        // 加工商
                        logoImageLabel.backgroundColor = FCRedBackColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"supply"]){
                        // 供应商
                        logoImageLabel.backgroundColor = FCOrangeColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"purchase"]){
                        // 供应商
                        logoImageLabel.backgroundColor = FCBlueTxtColor;
                        logoImageLabel.alpha = 0.6;
                    } else if([relationType.code isEqualToString:@"server"]){
                        // 供应商
                        logoImageLabel.backgroundColor = HEXCOLOR(0x1FA679);
                        logoImageLabel.alpha = 0.6;
                    } else{
                        // 其它
                        logoImageLabel.backgroundColor = HEXCOLOR(0xB87333);
                        logoImageLabel.alpha = 0.4;
                    }
                    if (self.relation.rel.BrandName.length > 2) {
                        NSString *logoStr = [self.relation.rel.BrandName substringToIndex:2];
                        logoImageLabel.text = logoStr;
                    } else{
                        logoImageLabel.text = self.relation.rel.BrandName;
                    }
                }
                
                UILabel *statusLabel = [UILabel new];
                [cell.contentView addSubview:statusLabel];
                [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-16);
                    make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                    make.height.mas_equalTo(22);
                }];
                statusLabel.font = font14;
                if ([self.relation.status isEqualToString:@"Request"]) {
                    statusLabel.textColor = FCBlueTxtColor;
                    statusLabel.text = customerRelationLocaChanged(@"已发送绑定请求", @"已发送绑定请求");
                }
                if ([self.relation.status isEqualToString:@"Reject"]) {
                    statusLabel.textColor = FCRedTxtColor;
                    statusLabel.text = customerRelationLocaChanged(@"请求被驳回", @"请求被驳回");
                }
                
                UILabel *companyNameLabel = [UILabel new];
                [cell.contentView addSubview:companyNameLabel];
                [companyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(statusLabel.mas_left).mas_equalTo(-9);
                    make.top.equalTo(avatarImageView.mas_top).mas_equalTo(0);
                    make.left.equalTo(avatarImageView.mas_right).mas_equalTo(9);
                    make.height.mas_equalTo(22);
                }];
                companyNameLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
                companyNameLabel.textColor = FCTxtColor;
                companyNameLabel.text = self.relation.rel.BrandName ?: @"";
                companyNameLabel.numberOfLines = 1;
                
                UILabel *mainBusinessLabel = [UILabel new];
                [cell.contentView addSubview:mainBusinessLabel];
                [mainBusinessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(companyNameLabel.mas_bottom).mas_equalTo(5);
                    make.left.equalTo(companyNameLabel.mas_left).mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                }];
                mainBusinessLabel.font = font14;
                mainBusinessLabel.textColor = FCGrayTxtDetailColor;
                mainBusinessLabel.numberOfLines = 0;
                if (self.relation.rel.CustomMainBusiness.length > 0) {
                    mainBusinessLabel.text = [NSString stringWithFormat:@"%@%@", customerRelationLocaChanged(@"主营：", @"主营："),self.relation.rel.CustomMainBusiness];
                } else{
                    mainBusinessLabel.text = customerRelationLocaChanged(@"主营：老板生意很好，还没添加主营信息", @"主营：老板生意很好，还没添加主营信息");
                }
                
                UIView *lineView1 = [UIView new];
                [cell.contentView addSubview:lineView1];
                [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(avatarImageView.mas_bottom).mas_equalTo(16);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView1.backgroundColor = FCBackColor;
                
                UIView *editRelationTypeView = [UIView new];
                [cell.contentView addSubview:editRelationTypeView];
                [editRelationTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView1.mas_bottom).mas_equalTo(0);
                    make.left.and.right.mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                }];
                UITapGestureRecognizer *editRelationTypeTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editRelationTypeAction:)];
                [editRelationTypeView addGestureRecognizer:editRelationTypeTapGesture];
                
                UIButton *relationTypeEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [editRelationTypeView addSubview:relationTypeEditButton];
                [relationTypeEditButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(46);
                    make.height.mas_equalTo(16);
                }];
                relationTypeEditButton.titleLabel.font = font10;
                [relationTypeEditButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [relationTypeEditButton setTitle:customerRelationLocaChanged(@"employee_department_edti", @"编辑") forState:UIControlStateNormal];
                [relationTypeEditButton setImage:customerRelationImageName(@"bt_edit") forState:UIControlStateNormal];
                relationTypeEditButton.backgroundColor = FCBlueBackColor;
                relationTypeEditButton.layer.cornerRadius = 2;
                [relationTypeEditButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - relationTypeEditButton.imageView.frame.size.width, 0, relationTypeEditButton.imageView.frame.size.width)];
                [relationTypeEditButton setImageEdgeInsets:UIEdgeInsetsMake(0, relationTypeEditButton.titleLabel.bounds.size.width + 3, 0, - relationTypeEditButton.titleLabel.bounds.size.width)];
                [relationTypeEditButton addTarget:self action:@selector(editRelationTypeAction:) forControlEvents:UIControlEventTouchUpInside];
                
                _relationType = [UILabel new];
                [editRelationTypeView addSubview:_relationType];
                [_relationType mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(relationTypeEditButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(relationTypeEditButton.mas_left).mas_equalTo(-10);
                }];
                _relationType.font = font14;
                _relationType.textColor = FCTxtColor;
                _relationType.numberOfLines = 1;
                NSString *typeStr = @"";
                for (FCRelationTypeModel *typeModel in self.relation.types) {
                    
                    typeStr = [NSString stringWithFormat:@"%@|%@",typeStr, typeModel.name];
                }
                if (typeStr.length > 1) {
                    typeStr = [typeStr substringFromIndex:1];
                }
                _relationType.text = typeStr;
                
                UIView *lineView2 = [UIView new];
                [cell.contentView addSubview:lineView2];
                [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(editRelationTypeView.mas_bottom).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView2.backgroundColor = FCBackColor;
                
                UIView *addressView = [UIView new];
                [cell.contentView addSubview:addressView];
                [addressView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView2.mas_bottom).mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                    make.left.and.right.mas_equalTo(0);
                }];
                UITapGestureRecognizer *addressTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushNav)];
                [addressView addGestureRecognizer:addressTapGesture];
                
                UIButton *addressButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [addressView addSubview:addressButton];
                [addressButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(46);
                    make.height.mas_equalTo(16);
                }];
                addressButton.titleLabel.font = font10;
                [addressButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [addressButton setTitle:customerRelationLocaChanged(@"global_location_location_navigate", @"导航") forState:UIControlStateNormal];
                [addressButton setImage:customerRelationImageName(@"bt_nav") forState:UIControlStateNormal];
                addressButton.backgroundColor = FCBlueBackColor;
                addressButton.layer.cornerRadius = 2;
                [addressButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - addressButton.imageView.frame.size.width, 0, addressButton.imageView.frame.size.width)];
                [addressButton setImageEdgeInsets:UIEdgeInsetsMake(0, addressButton.titleLabel.bounds.size.width + 3, 0, - addressButton.titleLabel.bounds.size.width)];
                [addressButton addTarget:self action:@selector(pushNav) forControlEvents:UIControlEventTouchUpInside];
                
                UILabel *address = [UILabel new];
                [addressView addSubview:address];
                [address mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(addressButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(addressButton.mas_left).mas_equalTo(-10);
                }];
                address.font = font14;
                address.textColor = FCTxtColor;
                address.numberOfLines = 1;
                address.text = [NSString stringWithFormat:@"%@%@%@%@%@",self.relation.rel.ResidentCountry ?: @"",self.relation.rel.ResidentProvince ?: @"",self.relation.rel.ResidentCity ?: @"",self.relation.rel.ResidentArea ?: @"",self.relation.rel.Address ?: @""];
                
                UIView *lineView3 = [UIView new];
                [cell.contentView addSubview:lineView3];
                [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(addressView.mas_bottom).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.mas_equalTo(-16);
                    make.height.mas_equalTo(1);
                }];
                lineView3.backgroundColor = FCBackColor;
                
                UIView *phoneView = [UIView new];
                [cell.contentView addSubview:phoneView];
                [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lineView3.mas_bottom).mas_equalTo(0);
                    make.left.and.right.mas_equalTo(0);
                    if (IphonePlus) {
                        make.height.mas_offset(54);
                    } else{
                        make.height.mas_equalTo(48);
                    }
                    make.bottom.mas_equalTo(0);
                }];
                UITapGestureRecognizer *phoneTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushTelPhone)];
                [phoneView addGestureRecognizer:phoneTapGesture];
                
                UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [phoneView addSubview:phoneButton];
                [phoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(-16);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(16);
                }];
                phoneButton.titleLabel.font = font10;
                [phoneButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                [phoneButton setTitle:customerRelationLocaChanged(@"company_card_takephone", @"打电话") forState:UIControlStateNormal];
                [phoneButton setImage:customerRelationImageName(@"bt_call") forState:UIControlStateNormal];
                phoneButton.backgroundColor = FCBlueBackColor;
                phoneButton.layer.cornerRadius = 2;
                [phoneButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - phoneButton.imageView.frame.size.width, 0, phoneButton.imageView.frame.size.width)];
                [phoneButton setImageEdgeInsets:UIEdgeInsetsMake(0, phoneButton.titleLabel.bounds.size.width + 3, 0, - phoneButton.titleLabel.bounds.size.width)];
                [phoneButton addTarget:self action:@selector(pushTelPhone) forControlEvents:UIControlEventTouchUpInside];
               
                UIView *telView = [FCCompanyCardTool telPhoneWithCompanyModelOwnerIdentityModel:self.relation.rel];
                [phoneView addSubview:telView];
                [telView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(phoneButton).mas_equalTo(0);
                    make.left.mas_equalTo(16);
                    make.right.equalTo(phoneButton.mas_left).mas_equalTo(-10);
                    make.height.mas_equalTo(20);
                }];
                
                return cell;
            } else {
                return [self meRelationCellWithIndexPath:indexPath.row];
            }
        }
    }
    if (indexPath.section == 1) {
        if (self.relation.relRelation) {
            return [self relRelationCellWithIndexPath:indexPath.row];
        } else {
            return [self meRelationCellWithIndexPath:indexPath.row];
        }
        
    }
    if (indexPath.section == 2) {
        return [self meRelationCellWithIndexPath:indexPath.row];
    }
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
    
    return cell;
}

-(void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

// 我司指派联系人
- (FCRelationDetailEmployeeTableViewCell *)meRelationCellWithIndexPath:(NSInteger )indexPath{
    NSDictionary *cache = [FCFuChengCacheTool getHttpCacheWithUrlString:@"crmSupplyRelationOnlyTypeManager" parameters:nil];
    FCCrmSupplyRelationOnlyTypeManagerModel *relationOnlyTypeManagerModel = [FCCrmSupplyRelationOnlyTypeManagerModel mj_objectWithKeyValues: cache];
    
    if (self.isMe) {
        NSMutableArray *arr = [NSMutableArray array];
        if (relationOnlyTypeManagerModel) {
            for (FCRelationRequestEmployeeModel *emp in self.relation.relRelation.emps) {
                if ([emp.employee.CompanyDepartment.Id isEqualToString:relationOnlyTypeManagerModel.companyDepartmentId]) {
                    [arr addObject:emp];
                }
            }
        } else{
            [arr addObjectsFromArray:self.relation.relRelation.emps];
        }
        
        FCRelationDetailEmployeeTableViewCell *cell = [[FCRelationDetailEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"employeeList" title:customerRelationLocaChanged(@"我司指派联系人", @"我司指派联系人") employees:arr isAddEmployee:YES];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
        cell.isShowDeleteButton = YES;
        WJCWeakSelf
        cell.deleteBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employeeModel = weakSelf.relation.relRelation.emps[row];
            [weakSelf deleteEmployeeWithEmployeeId:employeeModel.employeeId andEmployeeName:employeeModel.employee.Name];
        };
        cell.addNewRelationEmployeeBlock = ^(NSArray * _Nonnull newEmployee) {
            [weakSelf addNewEmployeeWithEmployeeIds:newEmployee];
        };
        cell.didSelectRowBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employeeModel = weakSelf.relation.relRelation.emps[row];
            UIViewController *con = [weakSelf pushAppControllerWithControllerName:@"FCCPersonalInfoController" andObjectData:@{@"userInfo": employeeModel.employee} andTitle:customerRelationLocaChanged(@"me_account_personal_information", @"个人信息")];
        };
        return cell;
    } else{
        NSMutableArray *arr = [NSMutableArray array];
        if (relationOnlyTypeManagerModel) {
            for (FCRelationRequestEmployeeModel *emp in self.relation.emps) {
                if ([emp.employee.CompanyDepartment.Id isEqualToString:relationOnlyTypeManagerModel.companyDepartmentId]) {
                    [arr addObject:emp];
                }
            }
        } else{
            [arr addObjectsFromArray:self.relation.emps];
        }
        
        FCRelationDetailEmployeeTableViewCell *cell = [[FCRelationDetailEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"employeeList" title:customerRelationLocaChanged(@"我司指派联系人", @"我司指派联系人") employees:arr isAddEmployee:YES];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
        cell.isShowDeleteButton = YES;
        WJCWeakSelf
        cell.deleteBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employeeModel = arr[row];
            [weakSelf deleteEmployeeWithEmployeeId:employeeModel.employeeId andEmployeeName:employeeModel.employee.Name];
        };
        cell.addNewRelationEmployeeBlock = ^(NSArray * _Nonnull newEmployee) {
            [weakSelf addNewEmployeeWithEmployeeIds:newEmployee];
        };
        cell.didSelectRowBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employeeModel = arr[row];
            UIViewController *con = [weakSelf pushAppControllerWithControllerName:@"FCCPersonalInfoController" andObjectData:@{@"userInfo": employeeModel.employee} andTitle:customerRelationLocaChanged(@"me_account_personal_information", @"个人信息")];
        };
        return cell;
    }
    
}

// 企业联系人
- (FCRelationDetailEmployeeTableViewCell *)relRelationCellWithIndexPath:(NSInteger )indexPath{
    if (self.isMe) {
        FCRelationDetailEmployeeTableViewCell *cell = [[FCRelationDetailEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyEmployeeList" title:self.relation.me.BrandName ?: @"" employees:self.relation.emps  isAddEmployee:NO];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
        cell.isShowRelationButton = YES;
        WJCWeakSelf
        cell.phoneBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employModel = weakSelf.relation.emps[row];
            // 拨打员工电话
            employModel.employee.callWithEmp;
        };
        cell.talkBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employModel = weakSelf.relation.emps[row];
            // 员工在线聊天
            UIViewController *con = [weakSelf pushAppControllerWithControllerName:@"FCCPersonalInfoController" andObjectData:@{@"userInfo": employModel.employee} andTitle:customerRelationLocaChanged(@"me_account_personal_information", @"个人信息")];
        };
        
        return cell;
    } else{
        FCRelationDetailEmployeeTableViewCell *cell = [[FCRelationDetailEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyEmployeeList" title:self.relation.rel.BrandName ?: @"" employees:self.relation.relRelation.emps  isAddEmployee:NO];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        BA_ViewBorderRadius(cell, 4, 0, UIColor.clearColor);
        cell.isShowRelationButton = YES;
        WJCWeakSelf
        cell.phoneBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employModel = weakSelf.relation.relRelation.emps[row];
            // 拨打员工电话
            employModel.employee.callWithEmp;
        };
        cell.talkBlock = ^(NSInteger row) {
            FCRelationRequestEmployeeModel *employModel = weakSelf.relation.relRelation.emps[row];
            // 员工在线聊天
            UIViewController *con = [weakSelf pushAppControllerWithControllerName:@"FCCPersonalInfoController" andObjectData:@{@"userInfo": employModel.employee} andTitle:customerRelationLocaChanged(@"me_account_personal_information", @"个人信息")];
        };
        
        return cell;
    }
}

#pragma mark - 编辑关系类型
- (void)editRelationTypeAction:(UIButton *)sender{
//    [SVProgressHUD show];
//    WJCWeakSelf
//    [FCCustomerRelationRequest getCrmSupplyRelationTypeWithArgs:@{@"enable": @(YES)} success:^(id resultObj) {
//        FCRelationTypeListModel *relationTypesModel = [FCRelationTypeListModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationTypes"]];
//
//        FCRelationTyleSheetViewController *sheetVC = [[FCRelationTyleSheetViewController alloc] init];
//        sheetVC.relationTypeArr = relationTypesModel.items;
//
//        sheetVC.completeBlock = ^(NSMutableArray<FCRelationTypeModel *> * _Nonnull selectedRelationTypeArr) {
//            [weakSelf changeRelationTypeWithNewTypes:selectedRelationTypeArr];
//        };
//        sheetVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//        sheetVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//
//        [SVProgressHUD dismiss];
//        [weakSelf presentViewController:sheetVC animated:YES completion:^{ }];
//    } failure:^(NSError *error) {
//        [SVProgressHUD dismiss];
//    }];
    
    //获取缓存数据
    WJCWeakSelf
    if ([FCEmployeeModel empCompanyIsMine]) {
        self.manageRelationTypes = [NSMutableArray array];
    } else {
        NSMutableDictionary *relationTypeParam = [NSMutableDictionary dictionary];
        relationTypeParam[@"isManage"] = @"YES";
        NSArray *manageRelationTypeCache = [FCFuChengCacheTool getHttpCacheWithUrlString:FCCrmSupplyRelationTypesDataKey parameters:relationTypeParam];
        self.manageRelationTypes = [FCRelationTypeModel mj_objectArrayWithKeyValuesArray:manageRelationTypeCache];
    }
    
//    NSArray *cache = [FCFuChengCacheTool getHttpCacheWithUrlString:FCCrmSupplyRelationTypesDataKey parameters:nil];
//    self.relationTypes = [FCRelationTypeModel mj_objectArrayWithKeyValuesArray:cache];
    if (self.relationTypes.count && self.manageRelationTypes.count) {
        [self showRelationTypeSelect];
    } else {
        if (!self.isLoadData) {
            
            [self getRelationType];
        }
        
    }
}

- (void)getRelationType{
    self.isLoadData = YES;
    
    NSNumber *take = @(15);
    NSDictionary *args = @{@"enable": @(YES),
                           @"take": take,
                           @"skip": @(self.relationTypes.count)
                           };
    
    WJCWeakSelf
    [FCCustomerRelationRequest getCrmSupplyRelationTypeWithArgs:args success:^(id resultObj) {
        FCRelationTypeListModel *relationTypesModel = [FCRelationTypeListModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationTypes"]];
        [weakSelf.relationTypes addObjectsFromArray:relationTypesModel.items];
        if (weakSelf.relationTypes.count >= relationTypesModel.pageInfo.total) {
            XMGLog(@"relationType 全部请求完成");
            weakSelf.isLoadData = NO;
            //缓存
            NSArray *cache = [FCRelationTypeModel mj_keyValuesArrayWithObjectArray:self.relationTypes];
            [FCFuChengCacheTool saveHttpCache:cache urlString:FCCrmSupplyRelationTypesDataKey parameters:nil];
            
            [self showRelationTypeSelect];
        } else {
            XMGLog(@"relationType 继续请求");
            [self getRelationType];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        XMGLog(@"%@", error);
        self.isLoadData = NO;
    }];
}

- (void)showRelationTypeSelect{
    WJCWeakSelf
    [FCCustomerRelationRequest relationOnlyTypeManagerWithArgs:@{} success:^(id resultObj) {
        FCCrmSupplyRelationOnlyTypeManagerModel *relationOnlyTypeManagerModel = [FCCrmSupplyRelationOnlyTypeManagerModel mj_objectWithKeyValues: resultObj[@"data"][@"crmSupplyRelationOnlyTypeManager"]];
        
        FCRelationTyleSheetViewController *sheetVC = [[FCRelationTyleSheetViewController alloc] init];
        sheetVC.relationTypeArr = self.relationTypes;
        sheetVC.selectedRelationTypeArr = self.selectedRelationTypeArr;
        
        if (relationOnlyTypeManagerModel) {
            sheetVC.managerRelationTypeArr = [self.relation.types mutableCopy];
        } else if(self.selectedRelationTypeArr.count == 0){
            sheetVC.selectedRelationTypeArr = [self.relation.types mutableCopy];
        }
        
        sheetVC.completeBlock = ^(NSMutableArray<FCRelationTypeModel *> * _Nonnull selectedRelationTypes) {
            NSString *typeStr = @"";
            weakSelf.selectedRelationTypeArr = selectedRelationTypes;
            [weakSelf changeRelationTypeWithNewTypes:weakSelf.selectedRelationTypeArr];
        };
        sheetVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        sheetVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        
        [SVProgressHUD dismiss];
        [self presentViewController:sheetVC animated:YES completion:^{ }];
    } failure:^(NSError *error) {
        FCRelationTyleSheetViewController *sheetVC = [[FCRelationTyleSheetViewController alloc] init];
        sheetVC.relationTypeArr = [self.relationTypes mutableCopy];
        sheetVC.selectedRelationTypeArr = self.selectedRelationTypeArr;
        sheetVC.managerRelationTypeArr = [self.relation.types mutableCopy];
        sheetVC.completeBlock = ^(NSMutableArray<FCRelationTypeModel *> * _Nonnull selectedRelationTypes) {
            NSString *typeStr = @"";
            weakSelf.selectedRelationTypeArr = selectedRelationTypes;
            [weakSelf changeRelationTypeWithNewTypes:weakSelf.selectedRelationTypeArr];
        };
        sheetVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        sheetVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        
        [SVProgressHUD dismiss];
        [self presentViewController:sheetVC animated:YES completion:^{ }];
    }];
}

- (void)changeRelationTypeWithNewTypes:(NSMutableArray<FCRelationTypeModel *> *)newTypes{
    NSMutableArray *removeTypeIdArr = [NSMutableArray array];
    NSMutableArray *addTypeIdArr = [NSMutableArray array];
    NSArray *newTypesArr = [NSArray arrayWithArray:newTypes];
    for (FCRelationModel *newType in newTypesArr) {
        [addTypeIdArr addObject:newType.Id];
    }
    for (FCRelationModel *type in self.relation.types) {
        BOOL isRemove = YES;
        
        for (FCRelationModel *newType in newTypesArr) {
            if([type.Id isEqualToString:newType.Id]){
                isRemove = NO;
            }
        }
        if (isRemove) {
            [removeTypeIdArr addObject:type.Id];
        }
    }
    NSMutableDictionary *args = [NSMutableDictionary dictionary];
    args[@"addTypeIds"] = addTypeIdArr;
    args[@"delTypeIds"] = removeTypeIdArr;
    if (self.isMe) {
        args[@"id"] = self.relation.relRelation.Id;
    } else {
        args[@"id"] = self.relation.Id;
    }
    
    WJCWeakSelf
    [FCCustomerRelationRequest editRelationWithArgs:args success:^(id resultObj) {
        FCRelationModel *relationModel = [FCRelationModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationEditAou"]];
//        FCRelationCompanyDetailViewController *relationBindRequestVC = [[FCRelationCompanyDetailViewController alloc] init];
        [[NSNotificationCenter defaultCenter] postNotificationName:CrmSupplyRelationChanged object:nil];
        weakSelf.relation = relationModel;
        [weakSelf.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 打负责人电话
- (void)pushTelPhone {
    if (self.isMe) {
        self.relation.me.OwnerIdentity.callWithEmp;
    } else{
        self.relation.rel.OwnerIdentity.callWithEmp;
    }
}

#pragma mark - 跳转导航 未完成
- (void)pushNav {
    if (self.isMe) {
        if ([self.relation.me.GeoLocation[@"lng"] doubleValue] && [self.relation.me.GeoLocation[@"lat"] doubleValue]) {
//            CLLocationCoordinate2D coordinate;
//            coordinate.longitude = [self.relation.me.GeoLocation[@"lng"] doubleValue];
//            coordinate.latitude = [self.relation.me.GeoLocation[@"lat"] doubleValue];
//
//            NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithCoordinate:coordinate andTitle:@"导航"];
//
//            id session=[self initWithObjectName:@"NIMKitLocationPoint" andObjectData:locationPoint];
//            [self pushAppControllerWithControllerName:@"XMGLocationViewController" andObjectData:@{@"locationPoint":locationPoint} andTitle:@"消息"];
            [self performObjbect:NSClassFromString(@"IMGetTool") andMethod:@"gotoLocationControllerWithLongitude:andLatitude:andController:" withObjects:[NSString stringWithFormat:@"%%@",self.relation.rel.GeoLocation[@"lng"]], [NSString stringWithFormat:@"%%@",self.relation.rel.GeoLocation[@"lat"]], self,nil];
            
        } else {
            NSString *location = [FCCompanyCardTool companyLocationWithCompanyModel:self.relation.me];
            NSString *addresss = [NSString stringWithFormat:@"%@%@",location,self.relation.me.Address];
            
            [self getCoordinaWithAddress:addresss andNav:self.navigationController];
        }
    } else {
        if ([self.relation.rel.GeoLocation[@"lng"] doubleValue] && [self.relation.rel.GeoLocation[@"lat"] doubleValue]) {
//            CLLocationCoordinate2D coordinate;
//            coordinate.longitude = [self.relation.rel.GeoLocation[@"lng"] doubleValue];
//            coordinate.latitude = [self.relation.rel.GeoLocation[@"lat"] doubleValue];
            
//            NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithCoordinate:coordinate andTitle:@"导航"];
//            id session=[self initWithObjectName:@"NIMKitLocationPoint" andObjectData:@{@"coordinate": coordinate, @"title": @"导航"}];
//            [self pushAppControllerWithControllerName:@"XMGLocationViewController" andObjectData:@{@"locationPoint":session} andTitle:@"消息"];
            
            [self performObjbect:NSClassFromString(@"IMGetTool") andMethod:@"gotoLocationControllerWithLongitude:andLatitude:andController:" withObjects:[NSString stringWithFormat:@"%%@",self.relation.rel.GeoLocation[@"lng"]], [NSString stringWithFormat:@"%%@",self.relation.rel.GeoLocation[@"lat"]], self,nil];
            
        } else {
            NSString *location = [FCCompanyCardTool companyLocationWithCompanyModel:self.relation.rel];
            NSString *addresss = [NSString stringWithFormat:@"%@%@",location,self.relation.rel.Address];
            
            [self getCoordinaWithAddress:addresss andNav:self.navigationController];
        }
    }
}
- (void)getCoordinaWithAddress:(NSString *)AddressName andNav:(UINavigationController *)nav {
    if (!AddressName.length) {
        return;
    }
    
    CLGeocoder *geoC = [[CLGeocoder alloc] init];
    WJCWeakSelf
    [geoC geocodeAddressString:AddressName completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if(error == nil) {
            
            CLPlacemark *obj = [placemarks firstObject];
            
            NSLog(@"%@", obj.name);
//            CLLocationCoordinate2D coordinate;
//            coordinate.latitude = obj.location.coordinate.latitude;
//            coordinate.longitude = obj.location.coordinate.longitude;
            
//            NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithCoordinate:coordinate andTitle:obj.name];
//            id session=[self initWithObjectName:@"NIMKitLocationPoint" andObjectData:@{@"coordinate": coordinate, @"title": @"导航"}];
//            [self pushAppControllerWithControllerName:@"XMGLocationViewController" andObjectData:@{@"locationPoint":session} andTitle:@"导航"];
            
            [weakSelf performObjbect:NSClassFromString(@"IMGetTool") andMethod:@"gotoLocationControllerWithLongitude:andLatitude:andController:" withObjects:[NSString stringWithFormat:@"%lf",obj.location.coordinate.longitude], [NSString stringWithFormat:@"%lf",obj.location.coordinate.latitude], weakSelf,nil];
            
        } else {
            
            [SVProgressHUD showErrorWithStatus:customerRelationLocaChanged(@"global_location_place_search_no_data", @"未搜索到相关位置")];
        }
        
    }];
    
}

#pragma mark - 删除联系人
- (void)deleteEmployeeWithEmployeeId:(NSString *)employeeId andEmployeeName:(NSString *)name{
    WJCWeakSelf
    [FCAlertViewController alertCommonWithTitle:[NSString stringWithFormat:@"确定删除“%@”关系类型？",name] andSubTitle:nil andController:weakSelf andCommitAction:^(FCAlertAction *action) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableDictionary *args = [NSMutableDictionary dictionary];
            args[@"delEmpIds"] = @[employeeId];
            if (self.isMe) {
                args[@"id"] = self.relation.relRelation.Id;
            } else{
                args[@"id"] = self.relation.Id;
            }
            
            [SVProgressHUD showWithStatus:customerRelationLocaChanged(@"删除联系人", @"删除联系人")];
            WJCWeakSelf
            [FCCustomerRelationRequest editRelationWithArgs:args success:^(id resultObj) {
                FCRelationModel *relationModel = [FCRelationModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationEditAou"]];
//                FCRelationCompanyDetailViewController *relationBindRequestVC = [[FCRelationCompanyDetailViewController alloc] init];
                weakSelf.relation = relationModel;
                weakSelf.isMe = false;
                [weakSelf.tableView reloadData];
                [SVProgressHUD showSuccessWithStatus:customerRelationLocaChanged(@"global_general_delete_success", @"删除成功")];
            } failure:^(NSError *error) {
                [SVProgressHUD dismiss];
            }];
        });
    }];
    
}

#pragma mark - 指派新联系人
- (void)addNewEmployeeWithEmployeeIds:(NSArray *)employeeIds{
    NSMutableDictionary *args = [NSMutableDictionary dictionary];
    args[@"addEmpIds"] = employeeIds;
    if (self.isMe) {
        args[@"id"] = self.relation.relRelation.Id;
    } else {
        args[@"id"] = self.relation.Id;
    }

    WJCWeakSelf
    [FCCustomerRelationRequest editRelationWithArgs:args success:^(id resultObj) {
        FCRelationModel *relationModel = [FCRelationModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationEditAou"]];
//        FCRelationCompanyDetailViewController *relationBindRequestVC = [[FCRelationCompanyDetailViewController alloc] init];
        weakSelf.relation = relationModel;
        weakSelf.isMe = false;
        [weakSelf.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 重新发起请求
- (void)resetRelationRequest{
    NSMutableDictionary *args = [NSMutableDictionary dictionary];
    args[@"request"] = @(YES);
    if (self.isMe) {
        args[@"id"] = self.relation.relRelation.Id;
    } else {
        args[@"id"] = self.relation.Id;
    }
    
    [SVProgressHUD showWithStatus:customerRelationLocaChanged(@"重新发送绑定请求", @"重新发送绑定请求")];
    WJCWeakSelf
    [FCCustomerRelationRequest editRelationWithArgs:args success:^(id resultObj) {
        FCRelationModel *relationModel = [FCRelationModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationEditAou"]];
//        FCRelationCompanyDetailViewController *relationBindRequestVC = [[FCRelationCompanyDetailViewController alloc] init];
        weakSelf.relation = relationModel;
        [weakSelf.tableView reloadData];
        [SVProgressHUD showSuccessWithStatus:customerRelationLocaChanged(@"global_general_send_succeed", @"发送成功")];
        [[NSNotificationCenter defaultCenter] postNotificationName:CrmSupplyRelationChanged object:nil];
    } failure:^(NSError *error) {
        
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 删除企业
- (void)deleteRelationCompany{
    NSMutableDictionary *args = [NSMutableDictionary dictionary];
    if (self.isMe) {
        args[@"ids"] = self.relation.relRelation.Id;
    } else {
        args[@"ids"] = self.relation.Id;
    }

    [SVProgressHUD showWithStatus:customerRelationLocaChanged(@"正在删除企业", @"正在删除企业")];
    WJCWeakSelf
    [FCCustomerRelationRequest deleteRelationRequestWithArgs:args success:^(id resultObj) {
        [SVProgressHUD dismiss];
        [[NSNotificationCenter defaultCenter] postNotificationName:CrmSupplyRelationChanged object:nil];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 获取联系人数据
- (void)getRelationData{
    NSDictionary *args = @{
                           @"id": self.relationId,
                           @"companyId": self.compangyId
                           };
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];
    WJCWeakSelf
    [FCCustomerRelationRequest getRelationDetailWithArgs:args success:^(id resultObj) {
        FCRelationModel *relationModel = [FCRelationModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationGet"]];
        weakSelf.relation = relationModel;
        if (weakSelf.isMe) {
            weakSelf.title = weakSelf.relation.me.Name ?: weakSelf.relation.relCompanyName;
        } else{
            weakSelf.title = weakSelf.relation.rel.Name ?: weakSelf.relation.relCompanyName;
        }
        if ([weakSelf.relation.status isEqualToString:@"Agree"]) {
            _bottomReviewView.height = 0.01;
            _bottomReviewView.y = WJCScreenH - FCTabbarSafeBottomMargin - 0.01;
        } else {
            _bottomReviewView.height = 80;
            _bottomReviewView.y = WJCScreenH - 80 - 64 - FCTabbarSafeBottomMargin;
        }
        if ([weakSelf.relation.status isEqualToString:@"Agree"]) {
            _bgview.height = WJCScreenH;
        } else {
            _bgview.height = WJCScreenH - FCTabbarHeight - FCNavBarHeight;
        }
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(23);
            if ([weakSelf.relation.status isEqualToString:@"Agree"]) {
                make.bottom.equalTo(weakSelf.bottomReviewView.mas_top).mas_equalTo(-64);
            } else {
                make.bottom.equalTo(weakSelf.bottomReviewView.mas_top).mas_equalTo(0);
            }
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
        }];
        [weakSelf.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

@end
